package com.agileai.pm.module.base.exteral;

import java.sql.Timestamp;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.pm.cxmodule.ProjectInfoManage;
import com.agileai.util.DateUtil;

public class ProjInfoImpl extends BaseRestService implements ProjInfo{

	@Override
	public String findProjectInfos() {
		ProjectInfoManage ProjectInfoManage=this.lookupService(ProjectInfoManage.class);
		User user=(User) getUser();
		DataParam param=new DataParam();
		param.put("userId",user.getUserId());
		List<DataRow> projectlist=ProjectInfoManage.findMobileRecords(param);
		JSONObject jsonObject = new JSONObject();
		try {
			JSONArray jsonArry = new JSONArray();
			for(DataRow row:projectlist){
				JSONObject tempJson = new JSONObject();
				
				String piName=row.getString("PI_NAME");
				String piType=row.getString("PI_TYPE");
				Timestamp piCreateTime=row.getTimestamp("PI_CREATE_TIME");
				String piCreatePeople=row.getString("PI_CREATE_PEOPLE");
				String piCreatePeopleValue=row.getString("PI_CREATE_PEOPLE_VALUE");
				String piRemarks=row.getString("PI_REMARKS");
				String piProjectAddres=row.getString("PI_PROJECT_ADDRES");
				String piSummary=row.getString("PI_SUMMARY");
				int piSumDay=row.getInt("PI_SUM_DAY");
				String piMailConfigure=row.getString("PI_MAIL_CONFIGURE");
				String piManager=row.getString("PI_MANAGER");
				String piId=row.getString("PI_ID");
				String piManagerValue=row.getString("PI_MANAGER_VALUE");
				String piTypeValue=row.getString("PI_TYPE_VALUE");
				
				tempJson.put("piName", piName);
				tempJson.put("piTypeValue", piTypeValue);
				tempJson.put("piCreatePeopleValue", piCreatePeopleValue);
				tempJson.put("piManagerValue", piManagerValue);
				tempJson.put("piType", piType);
				tempJson.put("piCreateTime",DateUtil.getDateByType(DateUtil.YYMMDDHHMI_HORIZONTAL,piCreateTime));   
				tempJson.put("piCreatePeople", piCreatePeople);
				tempJson.put("piRemarks", piRemarks);
				tempJson.put("piProjectAddres", piProjectAddres);
				tempJson.put("piSummary", piSummary);
				tempJson.put("piSumDay", piSumDay);
				tempJson.put("piMailConfigure", piMailConfigure);
				tempJson.put("piManager", piManager);
				tempJson.put("piId", piId);
				
				DataRow testRow=ProjectInfoManage.getActiveTest(piId);
				JSONObject testJson = new JSONObject();
				if(testRow!=null&&!testRow.isEmpty()){
					String ptName=testRow.getString("PT_NAME");
					String ptId=testRow.getString("PT_ID");
					testJson.put("ptName", ptName);
					testJson.put("ptId", ptId);
					testJson.put("piId", piId);
					tempJson.put("testJson",testJson);
					tempJson.put("isActiveTest",true);
			 	}else{
			 		tempJson.put("isActiveTest",false);
			 	}
				
				jsonArry.put(tempJson);
			}
			jsonObject.put("projectInfos", jsonArry );
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	@Override
	public String getActiveTestInfos() {
		ProjectInfoManage ProjectInfoManage=this.lookupService(ProjectInfoManage.class);
		JSONObject jsonObject = new JSONObject();
		DataRow testRow=ProjectInfoManage.getActiveTest("");
		 
		try {
			if(testRow!=null&&!testRow.isEmpty()){
				String ptName=testRow.getString("PT_NAME");
				String ptId=testRow.getString("PT_ID");
				String piId=testRow.getString("PI_ID");
				 
				jsonObject.put("ptName", ptName);
				jsonObject.put("ptId", ptId);
				jsonObject.put("piId", piId);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	}

	@Override
	public String getFunctionMenu(String projectId) {
		ProjectInfoManage ProjectInfoManage=this.lookupService(ProjectInfoManage.class);
		JSONObject jsonObject = new JSONObject();
		DataRow testRow=ProjectInfoManage.getActiveTest("");
		 
		try {
			if(testRow!=null&&!testRow.isEmpty()){
				String ptName=testRow.getString("PT_NAME");
				String ptId=testRow.getString("PT_ID");
				String piId=testRow.getString("PI_ID");
				 
				jsonObject.put("ptName", ptName);
				jsonObject.put("ptId", ptId);
				jsonObject.put("piId", piId);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject.toString();
	} 
}
